package com.codefish.codefish_blog.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codefish.codefish_blog.entity.ArchiveInfo;
import com.codefish.codefish_blog.entity.Article;
import com.codefish.codefish_blog.entity.ArticleSnapshot;
import com.codefish.codefish_blog.entity.ClassifyInfo;
import com.codefish.codefish_blog.mapper.ArticleMapper;
import com.codefish.codefish_blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author codefish
 * @since 2022-03-22
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public List<ArticleSnapshot> listArticleSnapshots(IPage<ArticleSnapshot> page) {
        return articleMapper.listArticleSnapshots(page);
    }

    @Override
    public List<ArticleSnapshot> listNewArticleSnapshots() {
        return articleMapper.listNewArticleSnapshots();
    }

    @Override
    public List<List<ArchiveInfo>> listSimplyOrderByDate() {
        List<ArchiveInfo> archiveInfos = articleMapper.listSimplyOrderByDate();
        int preYear = -1;
        List<List<ArchiveInfo>> list = new ArrayList<>();
        for (ArchiveInfo archiveInfo : archiveInfos) {
            if (archiveInfo.getYear() != preYear) {
                list.add(new ArrayList<>());
                preYear = archiveInfo.getYear();
            }
            list.get(list.size() - 1).add(archiveInfo);
        }
        return list;
    }

    @Override
    public List<ClassifyInfo> listArticleSnapshotsByCategory(String category) {
        List<ClassifyInfo> list = articleMapper.listArticleSnapshotsByCategory(category);
        //完善列表中的标签信息
        for (ClassifyInfo info : list) {
            String str = info.getArticleTags();
            String[] strs = str.split(",");
            info.setArticleTagList(Arrays.asList(strs));
        }
        return list;

    }
}
